<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import { caching, hussarAxiosRequestUtils, tenantUtils, tokenUtil } from 'hussar-base';
import store from '@/pages/index/store';
import { InitThemeVars } from 'ThemeBase';
import { LoginApi } from '@/api/loginApi';

export default {
  name: 'App',
  methods: {
    // 加载主题配置信息
    getThemeConfigStore() {
      store.dispatch('HussarThemeConfigStore/loadScheme_server').then((curScheme) => {
        if (curScheme) {
          InitThemeVars(); // 初始化主题样式变量
        } else {
          console.log('主题样式获取异常');
        }
      }).catch((err) => {
        console.log(`主题样式获取异常${err}`);
      });
    },

    checkIsNameDiff(path, name) {
      name = decodeURI(name);
      this.$store.commit('tagsView/UPDATE_VISITED_VIEW_NAME', {
        path: path,
        name
      });
    },
    getTenantType() {
      hussarAxiosRequestUtils.getAction(LoginApi.getTenantConfig).then((res) => {
        if (res.code === 10000) {
          caching.local.set('tenantType', res.data.type);
          if (res.data.type && window.tenantType !== res.data.type) {
            console.warn('前后台的租户类型不匹配');
          }
        }
        this.getThemeConfigStore();
      });
    }
  },
  mounted() {
    window.addEventListener('message', (e) => {
      const trustedOrigins = location.origin;
      if (e.origin === trustedOrigins && e.data && e.data.name && e.data.path) {
        this.$store.dispatch('GenerateRoutes', null).then(() => { // 根据roles权限生成可访问的路由表
          const name = e.data.name;
          const path = e.data.path;
          this.checkIsNameDiff(path, name);
        });
      }
    });
  },
  created() {
    this.getTenantType();
  }
};
</script>
<style>
@import "~@/assets/font/font.css";
</style>
<style>
/*body{*/
/*  margin:0px;*/
/*  width:100%;*/
/*  min-width:1500px;*/
/*  max-width:100%;*/
/*  height:100%;*/
/*  background-color:#F0F0F0;*/
/*}*/
</style>
